import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


/* google how to use multiple screens.
 * Multiple screens are going to be used to make Title, Selection, and Battle scene.
 * http://www.gamedev.net/topic/147616-rendering-multiple-quotscreensquot-in-an-applet/
 * Above URL is a good place to start.
 * - Yulo
 */


//Modified Player Class that has not merged with Quynh 

public class GameFieldApp extends Applet implements KeyListener, Runnable {
	
	private Player p1;
	private Player p2;
	private BufferedImage[] spritesP1;

	private int width = 400; // default == 400
	private int height = 250; // default == 250
	
	private int count;
	
	Thread runner;
	int frame; // In fighting game, the common FPS is 60.
	String message = "Final Fighters!!!";
	BufferedImage imageA; //Place holder image for now
	
	
	public void init() {
		setSize(width, height);
		
		try {
			imageA = ImageIO.read(new File("UrbanCity-Flatten.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			p1 = new RedPlayer(60, height - 100, true, 10, 15, 5, 9, 12, 16);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		spritesP1 = new BufferedImage[44];
		loadSprites(44);
		try {
			p2 = new BluePlayer(width - 60, height - 100, false, 13, 18, 4, 8, 10, 16);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// Animation starts here.
		runner = new Thread(this);
		runner.start();
		
		// Added actions goes under here.
		addKeyListener(this);
		
	}
	
	public void paint(Graphics g) {
		g.drawImage(imageA, 0 - width/2, 0 - height - 90, null);
		//g.drawLine(0, height - 30, width, height - 30); base line
		
		g.setColor(p1.getColor());
		g.fillOval(p1.getPoint().getX(), p1.getPoint().getY(), 20, 20);
		//g.drawImage(spritesP1[1], p1.getPoint().getX(),  p1.getPoint().getY(), null);
		lifeBar(p1, g);
		energyBar(p1, g);
		
		g.setColor(p2.getColor());
		g.fillOval(p2.getPoint().getX(), p2.getPoint().getY(), 20, 20);
		lifeBar(p2, g);
		energyBar(p2, g);
		
		showStatus(message);
		
		g.setColor(Color.WHITE);
		
		// Displays message when player(s) are dead
		if(p1.isDead() && p2.isDead()){
			g.drawString("What is this!? Both Players have lost!!!", width/2 - 50, (int)(height/3));
		} else if(p1.isDead()){
			g.drawString("Player 1 lose!", width/2, (int)(height/3));
		} else if(p2.isDead()){
			g.drawString("Player 2 lose!", width/2, (int)(height/3));
		}
	}
	
	@Override
	public void run() {
		while(true) {
			// Every second, increase energy bar by 1 point
			// - Quynh
			repaint();
			
			// Checks if either playing is jumping, player then ascends slowly and descends quickly.
		
			if(p1.isJumping()) {
				if(p1.getJumpPixels() < p1.getJumpHeight() && p1.getiFactor() >= -p1.getiDefault()) {
					p1.setPoint(new Point(p1.getPoint().getX(), p1.getPoint().getY() - p1.getiFactor()));
					p1.setJumpPixels(p1.getJumpPixels() + 1);
					p1.setiFactor(p1.getiFactor() - 1);
				} if(p1.getiFactor() < -p1.getiDefault()) {
					p1.setJumping(false);
					p1.setiFactor(p1.getiDefault());
					p1.setJumpPixels(0);
				}
			}
			if(p2.isJumping()) {
				if(p2.getJumpPixels() < p2.getJumpHeight() && p2.getiFactor() >= -p2.getiDefault()) {
					p2.setPoint(new Point(p2.getPoint().getX(), p2.getPoint().getY() - p2.getiFactor()));
					p2.setJumpPixels(p2.getJumpPixels() + 1);
					p2.setiFactor(p2.getiFactor() - 1);
				} if(p2.getiFactor() < -p2.getiDefault()) {
					p2.setJumping(false);
					p2.setiFactor(p2.getiDefault());
					p2.setJumpPixels(0);
				}
			}
			
			if((count % 1) == 0) {
				if(p1.getEnergyPoints() != p1.getMaxLimitEP()) {
					p1.setEnergyPoints(p1.getEnergyPoints() + 1);
				}
				
				if(p2.getEnergyPoints() != p2.getMaxLimitEP()) {
					p2.setEnergyPoints(p2.getEnergyPoints() + 1);
				}
			}
			
			if((count % 200) == 0) {
				
			}
			
			try {
				Thread.sleep(100);
				count++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	// Battle Scene props goes under here.
	
	public void loadSprites(int number){
		for(int i = 0; i <= number; i++){
			try {
				spritesP1[i]= ImageIO.read(new File("RedPlayer/" + i +".png"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	
	public void lifeBar(Player p, Graphics g){
		if(p.getTeam())
			g.fillRect(0, 0, (int)(p.getLifePoints() * 1.5), 25);
		
		if(!p.getTeam())
			g.fillRect(width, 0, (int)(-p.getLifePoints() * 1.5), 25);
	}
	
	public void energyBar(Player p, Graphics g){
		if(p.getTeam())
			g.fillRect(0, 35, p.getEnergyPoints(), 10);
		
		if(!p.getTeam())
			g.fillRect(width, 35, -(p.getEnergyPoints()), 10);
	}

	/* Defines the direction controls for each player and resets locations if method is called
	 * Player 1 = Arrow Keys
	 * Player 2 = W, A, S, D
	 */
	
	public void moveP(Player player, int keyCode) throws InterruptedException {
		if(keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A){ // Move left
			if(player.getPoint().getX() - 5 > 10){
				player.setPoint(new Point(player.getPoint().getX() - 5, player.getPoint().getY()));
				repaint();
			}
		} else if(keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W){ //Jump
			player.setJumping(true);

		} else if(keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D){ //Move right
			if(player.getPoint().getX() + 5 < width - 30){
				player.setPoint(new Point(player.getPoint().getX() + 5, player.getPoint().getY()));
				repaint();
			}
		} else if(keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S){ //Block
			System.out.println("Block!");
		}
	}
	
	
	// Action methods under here.
	
	// KeyListener checks to see which key is pressed, then calls the generic move method
	
	@Override
	public void keyPressed(KeyEvent e) {
		
		// PLAYER 1 CONTROLS
		
		if(e.getKeyCode() == KeyEvent.VK_UP
		|| e.getKeyCode() == KeyEvent.VK_RIGHT
		|| e.getKeyCode() == KeyEvent.VK_DOWN
		|| e.getKeyCode() == KeyEvent.VK_LEFT){
			try {
				moveP(p1, e.getKeyCode());
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		if(e.getKeyCode() == KeyEvent.VK_COMMA) {
			//p1.setLifePoints(p1.getLifePoints() - 10);
			p1.kick(p2);
			System.out.println("kick!");
			repaint();
		}
		if(e.getKeyCode() == KeyEvent.VK_PERIOD) {
			// p2.setLifePoints(p2.getLifePoints() - 10);
			p1.punch(p2);
			repaint();
		}
		if(e.getKeyCode() == KeyEvent.VK_SLASH) {
/*			p1.setLifePoints(p1.getLifePoints() - 10);
			p2.setLifePoints(p2.getLifePoints() - 10);*/
			p1.special(p2);
			repaint();
		}
		
		// PLAYER 2 CONTROLS
		
		if(e.getKeyCode() == KeyEvent.VK_W
				|| e.getKeyCode() == KeyEvent.VK_A
				|| e.getKeyCode() == KeyEvent.VK_S
				|| e.getKeyCode() == KeyEvent.VK_D){
					try {
						moveP(p2, e.getKeyCode());
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
		}
		
		if(e.getKeyCode() == KeyEvent.VK_G) {
			p2.kick(p1);
			System.out.println("kick!");
			repaint();
		}
		if(e.getKeyCode() == KeyEvent.VK_H) {
			p2.punch(p1);
			repaint();
		}
		if(e.getKeyCode() == KeyEvent.VK_J) {
			p2.special(p1);
			repaint();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		// jump method
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}
